{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import logging\n",
    "import torch\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from PIL import Image, ImageDraw, ImageFont\n",
    "from matplotlib import pyplot as plt\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Keywords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = Path('/remote-home/share/medical/public/ROCO')\n",
    "\n",
    "keywords_test = root_dir / 'test/radiology/keywords.txt'\n",
    "keywords_valid = root_dir / 'valid/radiology/keywords.txt'\n",
    "keywords_train = root_dir / 'train/radiology/keywords.txt'\n",
    "\n",
    "csv_test = root_dir / 'test/radiology/processed_test.csv'\n",
    "csv_valid = root_dir / 'valid/radiology/processed_valid.csv'\n",
    "csv_train = root_dir / 'train/radiology/processed_train.csv'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(keywords_test) as f:\n",
    "    s_test = f.readlines()\n",
    "\n",
    "with open(keywords_valid) as f:\n",
    "    s_valid = f.readlines()\n",
    "\n",
    "with open(keywords_train) as f:\n",
    "    s_train = f.readlines()\n",
    "\n",
    "s = s_test + s_valid + s_train\n",
    "\n",
    "id_caption = {}\n",
    "for line in s:\n",
    "    words = line.strip().split()\n",
    "    id_caption[words[0]] = ' '.join(words[1:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'axial coronal mri view'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_caption['ROCO_00001']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Combine keywords to captions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test = pd.read_csv(csv_test, sep=',')\n",
    "df_valid = pd.read_csv(csv_valid, sep=',')\n",
    "df_train = pd.read_csv(csv_train, sep=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def keywords_to_caption(df_data, id_caption):\n",
    "    for index, row in tqdm(df_data.iterrows(), desc='keywords->caption'):\n",
    "        caption = id_caption[row['id']]\n",
    "        df_data.loc[index, 'caption'] = caption\n",
    "    return df_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "keywords->caption: 8176it [00:01, 6375.53it/s]\n",
      "keywords->caption: 8175it [00:01, 6518.19it/s]\n",
      "keywords->caption: 65419it [00:10, 6462.39it/s]\n"
     ]
    }
   ],
   "source": [
    "df_new_test = keywords_to_caption(df_data=df_test, id_caption=id_caption)\n",
    "df_new_valid = keywords_to_caption(df_data=df_valid, id_caption=id_caption)\n",
    "df_new_train = keywords_to_caption(df_data=df_train, id_caption=id_caption)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new_test.to_csv('/remote-home/share/medical/public/ROCO/test/radiology/key2cap_test.csv', sep=',', index=False)\n",
    "df_new_valid.to_csv('/remote-home/share/medical/public/ROCO/valid/radiology/key2cap_valid.csv', sep=',', index=False)\n",
    "df_new_train.to_csv('/remote-home/share/medical/public/ROCO/train/radiology/key2cap_train.csv', sep=',', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
